package com.sname.car.user.controller;
import com.sname.car.common.constants.Constants;
import com.sname.car.common.exception.CarException;
import com.sname.car.common.resp.ApiResult;
import com.sname.car.common.rest.BaseController;
import com.sname.car.user.entity.LoginInfo;
import com.sname.car.user.entity.User;
import com.sname.car.user.entity.UserElement;
import com.sname.car.user.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * Created By  醉美柳舞之众星捧月
 * @author songx
 * @date 2018/9/21 22:22
 */
@RestController
@RequestMapping("user")
public class UserController extends BaseController {
    private Logger log = LoggerFactory.getLogger(getClass());
    @Autowired
    @Qualifier("userServiceImpl")
    private UserService userService;
    /**
     * Author song  醉美柳舞之众星捧月
     * Date & Time  2018/9/25 10:15
     * Description  用户登录
     */
    @RequestMapping("/login")
    public ApiResult login(@RequestBody @Valid LoginInfo loginInfo) {
        ApiResult<Object> resp = new ApiResult<>();
        try {
            String LoginData = loginInfo.getData();
            String LoginKey = loginInfo.getKey();
            if (StringUtils.isBlank(LoginData) || StringUtils.isBlank(LoginKey)) {
                throw new CarException(Constants.ARGS_ERROR);
            }
            String token = userService.login(LoginData, LoginKey);
            resp.setData(token);
        } catch (CarException e1) {
            resp.setCode(Constants.HTTP_ERROR_CODE);
            resp.setMessage(e1.getMessage());
            resp.setSuccess(false);
        }
        return resp;
    }
    /**
     * Author song  醉美柳舞之众星捧月
     * Date & Time  2018/9/25 10:15
     * Description  用户修改昵称
     */
    @RequestMapping("/modifyNickName")
    public ApiResult modifyNickName(@RequestBody @Valid User user) {
        ApiResult<Object> resp = new ApiResult<>();
        try {
            /** 修改信息的时候,需要判断此用户是否是本人;不能相信从外界传入的数据,应该从缓存中去获取登录的这个人的数据 **/
            UserElement userElement = getCurrentUserInformation();
            user.setId(userElement.getUserId());
            int result = userService.modifyNickName(user);
            if (result <= 0) {
                throw new CarException("修改用户昵称失败！");
            }
            resp.setData(true);
        } catch (Exception e) {
            log.error("Fail to modify user nicName", e);
            resp.setCode(Constants.HTTP_ERROR_CODE);
            resp.setMessage(e.getMessage());
        }
        return resp;
    }
    /**
     * Author song  醉美柳舞之众星捧月
     * Date & Time  2018/9/25 10:15
     * Description  发送短信验证码
     */
    @RequestMapping("/sendVercode")
    public ApiResult sendVerCode(User user, HttpServletRequest request) {
        ApiResult<Object> resp = new ApiResult<>();
        try {
            Boolean isSuccess = userService.sengVerCode(user.getMobile(), getIpFromRequest(request));
        } catch (Exception e) {
            log.error("Fail to send ver coder", e);
            resp.setCode(Constants.HTTP_ERROR_CODE);
            resp.setMessage(e.getMessage());
        }
        return resp;
    }
    /**
     * Author song  醉美柳舞之众星捧月
     * Date & Time  2018/9/25 15:03
     * Description  用户上传头像
     */
    @RequestMapping("/upload")
    public ApiResult uploadUserImg(@RequestParam(required = false) MultipartFile file) {
        ApiResult<Object> resp = new ApiResult<>();
        try {
            UserElement userElement = getCurrentUserInformation();
            String result = userService.uploadUserImg(file, userElement.getUserId());
            if (StringUtils.isBlank(result)) {
                throw new CarException("上传用户头像失败！");
            }
            resp.setData(true);
        } catch (Exception e) {
            log.error("Fail to upload user Head Img", e);
            resp.setCode(Constants.HTTP_ERROR_CODE);
            resp.setMessage(e.getMessage());
        }
        return resp;
    }
}
